<template>
  <div class="transferAccount">
    <div class="headerBox">
      <van-nav-bar
        title="转账"
        left-arrow
        @click-left="goBack"
      ></van-nav-bar>
    </div>
    <div class="formBox">
      <van-cell title="付款钱包" class="cellDropDown" is-link :value="walletSelect.walletName" @click="showPicker=true">
      </van-cell>
      <van-cell title="当前FIL数" :value="balance" class="subAllot" />
      <van-field v-model="ruleForm.value" type="number" label="转出数量" oninput="if(value.length>8)value=value.slice(0,10)" placeholder="请输入转出数量" input-align="right" />
      <!-- <div class="cellInput">
        <span class="label">转出数量</span>
        <MyNumberInput :point="2" :max="999999999" placeholder="请输入转出数量" v-model.number="ruleForm.value"></MyNumberInput>
      </div> -->
      <div class="addressBox">
        <van-field v-model="ruleForm.to" label="钱包地址" placeholder="请输入钱包地址" input-align="right" />
        <img src="@/assets/img/wallet/icon_saomiao@2x.png" alt="" class="scannerIcon" @click="scannerFunc">
      </div>
      <van-field type="password" v-model="payPwd" label="支付密码" placeholder="请输入支付密码" input-align="right" />
      <p></p>
    </div>
    <div class="handlerBox" v-show="isOriginHei">
      <p class="handlerBtn handlerBtn1" @click="submit">提 交</p>
    </div>
    <van-popup v-model="showPicker" position="bottom" get-container="body">
      <van-picker
        show-toolbar
        :columns="walletList"
        @cancel="showPicker = false"
        @confirm="onConfirm"
        value-key="walletName"
      />
    </van-popup>
  </div>
</template>

<script>
import {Encrypt,Decrypt} from '@/utils/AES.utils.js'
import {getRsaCode} from '@/utils/RSA.utils.js'
import md5 from 'js-md5';
import FNScanner from "../FNScanner.js";
import positionBtn from "../positionBtn.js";
import MyNumberInput from '../../components/MyNumberInput';
export default {
  mixins: [FNScanner,positionBtn],
  data() {
    return {
      balance:0,
      walletList: [],
      payPwd:'',
      ruleForm:{
        to:'',
        value:'',
      },
      walletSelect:{},
      showPicker:false,
    }
  },
  components: {
        MyNumberInput //注册 
  },
  created(){
    // this.getUserWalletList()
    // let d= {"359":{"f3xd3lln5rtk3gddb5k27gumlkrs5bdupwqfhnmyoegf6mhj7ie4rgg3f477hegfykffldy62ausirfj565gcq":{"payPwd":"LapHSJJWogeVwQ71ENfE4w=="},"f3s2ui55uytjnkrxox4ig3tx7szx2wdq4vq4ygfzn473ntjqyv23bju2y7sf23wu6x4kh2sehbb7skpfjvtaka":{"payPwd":"ArKN/udfLZCqEYr8HUW3XQ==","secretKey":"bIgQOWQqDRAJeOOUDJ3QKFI7WipEwA6txJG1W2JC+N1grZkGbF6Veydbdp2VvIi3/0u0fWUR+PER\nXci+FIud1Td1Sl5LjKVAqQhSO752SLo2VfXB7WeuMHhck/0naCGz+YmLbno6xmgjyWklWwxTyuuR\nBWnFOacOO8k8idGn5FuTwxHdRKulO6uBSYGInGDl9LcpLGKhiGLcmfByJdAyCA=="},"f3spssrfq2ugv4o6rbwnthkvpuripjrhn6y4bkc7lz2dt2zuu77t24hxmun754xjhoeh4emsd66bq5sxj5sa2a":{"payPwd":"LapHSJJWogeVwQ71ENfE4w=="},"f3riwnobylqingmu3kth6m6fptzz5hsmcxpvai5ct4rlshyhzunkci4jlgdevzfnt6oleb3uv5foefvahkvytq":{"payPwd":"SRQLfKZHhzKsI8JPLeYqSw=="}}}
    // localStorage.setItem('wallet',JSON.stringify(d))
  },
  watch: {
    scannerContent(val) {
      this.ruleForm.to=val
    }
  },
  methods:{
    onConfirm(value, index) {
      this.showPicker = false;
      this.walletSelect=value;
      this.getUserFilInfo()
    },
    async getUserFilInfo(){
        this.$toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration:5000
        });
        let res = await this.$http.userWalletBalance({walletAddr:this.walletSelect.walletAddr});
        if (res) {
          this.balance = this.$selfFunc.toNonExponential(res.data.balance,8)
          this.$toast.clear()
        } else{
          this.$toast.clear()
        }
    },
    async getUserWalletList(){
        this.$toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration:5000
        });
        let res = await this.$http.getUserWalletList();
        if (res) {
          this.walletList =res.data.map(x=>{
            x.text = x.walletName
            x.value = x.walletAddr
            if(x.walletStatus) {
              this.walletSelect=x
            }
            return x
          })
          this.getUserFilInfo()
        }
    },
    goBack(){
      this.$router.go(-1)
    },
    async submit(){
      this.$toast('你还没有创建钱包')
      return
      let userId=JSON.parse(localStorage.getItem('userInfor')).userId
      let wallet= JSON.parse(localStorage.getItem('wallet'))[userId][this.walletSelect.walletAddr]
      console.log(Decrypt(wallet.payPwd))
      if(Number(this.balance)==0){
        this.$toast('当前fil数量不足')
        return
      } else if(!this.ruleForm.value) {
        this.$toast('请输入转出数量')
        return
      } else if(Number(this.ruleForm.value)>Number(this.balance)) {
        this.$toast('转出数量超过当前FIL数')
        return
      }  else if (!this.ruleForm.to) {
        this.$toast('请输入或者扫码填入钱包地址')
        return
      } else if(!this.payPwd) {
        this.$toast('请输入支付密码')
        return
      } else if (!wallet) {
        this.$toast('钱包数据丢失')
        return
      } else if(this.payPwd!=Decrypt(wallet.payPwd)) {
        this.$toast('支付密码错误')
        return
      }
      this.$toast.loading({
          message: '提交中...',
          forbidClick: true,
          duration:5000
      });
      let parmas={
        transferWalletAddr:this.walletSelect.walletAddr,
        collectWalletAddr:this.ruleForm.to,
        transferAmount:this.ruleForm.value,
        handlingFee:0
      }
      let sign = `collectWalletAddr=${this.ruleForm.to}&handlingFee=0&transferAmount=${this.ruleForm.value}&transferWalletAddr=${this.walletSelect.walletAddr}`
      parmas.sign = getRsaCode(md5(sign))
      let res = await this.$http.addTransferDetails(parmas);
      if (res) {
        this.payPwd=""
        this.ruleForm.to=""
        this.ruleForm.value=""
        this.$toast.clear()
        this.$toast('提交成功')
      } else{
        this.$toast.clear()
      }
    },
    objKeySort(obj) {
        var newkey = Object.keys(obj).sort();
        var newObj = {};
        for (var i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]];
        }
        return newObj;
    },
    scannerFunc(){
      this.FNScannerFunc()
    },
  }
};
</script>

<style scoped>
.transferAccount{
  background: #fff;
  min-height: 100vh;
}
.handlerBox{
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #fff;
  border-top: 2px solid #F1F1F1;
}
.handlerBtn{
  height: 0.82rem;
  line-height: 0.82rem;
  border-radius: 0.41rem;
  text-align: center;
  color: #fff;
  font-size:0.34rem;
  margin: 0 auto;
  margin-top: 0.2rem;
  margin-bottom: 0.2rem;
}
.handlerBtn1{
  width: 6.94rem;
  background: #F6741F;
}
.addressBox{
  position: relative;
}
.scannerIcon{
  position: absolute;
  width: 0.4rem;
  height: 0.4rem;
  top:0rem;
  right: 0rem;
  padding: 0.22rem 0.28rem 0.2rem 0.2rem;
  box-sizing: content-box;
}
</style>
<style>
.subAllot .van-cell__value{
  font-size: 0.34rem;
  color: #333;
  font-weight: bold;
}
.addressBox input{
  padding-right: 0.55rem;
}
</style>